package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/01user01")
public class SysUserController1 {
    private Logger logger = Logger.getLogger(SysUserController1.class);
    private ArrayList<SysUser> sysUserList = new ArrayList<>();
    //用于返回查询到的用户数据
    private ArrayList<SysUser> queryUserList = new ArrayList<>();
    public SysUserController1(){
        try{
            sysUserList.add(new SysUser(5,"zhangjing","张静","5555555",1,"13054784445",
                    "上海市宝山区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(4,"wanglin","王琳","4444444",1,"18965652364",
                    "北京市朝阳区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(1,"test001","测试用户001","11111111",1,"13566669998",
                    "北京市学院路",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(2,"zhaoyan","赵燕","2222222",1,"18678786545",
                    "北京市海淀区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(3,"test003","测试用户003","1111111",1,"13121334531",
                    "北京市海淀区",1,1,new Date(),1,new Date()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //查询所有用户
    @GetMapping("/list")
    public String list(Model model){
        logger.info("当查询条件为空时，查询用户信息");
        queryUserList.clear();
        queryUserList.addAll(sysUserList);
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }

    //根据条件查询部分用户
    @PostMapping("/list")
    public String list(@RequestParam(value = "realName" ,required = false)String realName,Model model){
        logger.info("查询条件：realName="+realName+"，查询用户的信息");
        queryUserList.clear();
        if(realName!=null&&!realName.equals("")){
            for (SysUser user:sysUserList){
                if(user.getRealName().indexOf(realName)!=-1){
                    queryUserList.add(user);
                }
            }
        }else {
            queryUserList.addAll(sysUserList);
        }
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }
}
